package com.xmcowell.sportpacemaster.base;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.provider.Settings;


import com.king.thread.nevercrash.NeverCrash;
import com.xmcowell.sportpacemaster.config.Contents;
import com.xmcowell.sportpacemaster.layout.SerialPortService;
import com.xmcowell.sportpacemaster.layout.entity.UserPlanSet;
import com.xmcowell.sportpacemaster.layout.entity.UserPlanSetDao;
import com.xmcowell.sportpacemaster.layout.login.WelcomeActivity;
import com.xmcowell.sportpacemaster.layout.setting.language.LanguageUtils;
import com.xmcowell.sportpacemaster.utils.DaoManager;
import com.xmcowell.sportpacemaster.utils.DownloadUtils;
import com.xmcowell.sportpacemaster.utils.PreferencesUtils;
import com.xmcowell.sportpacemaster.utils.mqtt.MyMqttSet;
import com.xmcowell.sportpacemaster.utils.retrofit.APIFactory;

import java.lang.reflect.Method;

/**
 * Created by xmsz on 2016/11/17.
 */

public class MyApplication extends Application {

    private static MyApplication instatnce;

    @Override
    public void onCreate() {
        super.onCreate();
        LanguageUtils.setLocale(this);

        instatnce = this;
        //初始化网络请求工具
        APIFactory.getInstance().init(this);
        Contents.init(this);
        DownloadUtils.init(this);
        DaoManager.getInstance().init(this);


//        //获取设备id
//        String deviceID = Settings.System.getString(getContentResolver(), Settings.System.ANDROID_ID);
////        ToastUtil.show(WelcomeActivity.this, deviceID + "_LOGIN");
//        MyMqttSet.start_PushService(this, deviceID + "_LOGIN");


        Intent intent = new Intent(this, SerialPortService.class);
        startService(intent);
        Intent mintent = new Intent(this, USBService.class);
        startService(mintent);

        //每次启动的时候，都设为未登录
        PreferencesUtils.putBooleanToSPMap(this, Contents.IS_LOGIN,false);

//        startService(new Intent("com.bodi.processkiller.PROCESS_KILL"));

//        banStatusBar(instatnce);
        NeverCrash.init(new NeverCrash.CrashHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {

            }
        });
    }
    public static MyApplication getInstatnce() {
        return instatnce;
    }

    public static final int DISABLE_EXPAND = 0x00010000;//4.2以上的整形标识
    public static final int DISABLE_EXPAND_LOW = 0x00000001;//4.2以下的整形标识
    public static final int DISABLE_NONE = 0x00000000;//取消StatusBar所有disable属性，即还原到最最原始状态
    /**
     * 通过反射机制，禁止statusBar下拉
     */
    public static void banStatusBar(Context context) {//禁止statusbar下拉，适配了高低版本
        int currentApiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentApiVersion <= 16) {
            setStatusBarDisable(context, DISABLE_EXPAND_LOW);
        } else {
            setStatusBarDisable(context, DISABLE_EXPAND);
        }
    }
    public static void setStatusBarDisable(Context context, int disable_status) {
        try {
            Object service = context.getSystemService("statusbar");
            Class<?> statusBarManager = Class.forName
                    ("android.app.StatusBarManager");
            Method expand = statusBarManager.getMethod("disable", int.class);
            expand.invoke(service, disable_status);
        } catch (Exception e) {
//            unBanStatusBar(context);
            e.printStackTrace();
        }
    }
    public static void unBanStatusBar(Context context) {//利用反射解除状态栏禁止下拉
        Object service = context.getSystemService("statusbar");
        try {
            Class<?> statusBarManager = Class.forName
                    ("android.app.StatusBarManager");
            Method expand = statusBarManager.getMethod("disable", int.class);
            expand.invoke(service, DISABLE_NONE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

    }

}
